package org.jmh.gift.service.impl;

import org.jmh.gift.domain.AccountAlipay;
import org.jmh.gift.domain.PayOrder;
import org.jmh.gift.dto.PcWebPayDTO;
import org.jmh.gift.service.IAccountAlipayService;
import org.jmh.gift.service.IPayOrderService;
import org.jmh.gift.service.IPayService;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayServiceImpl implements IPayService {

    @Autowired
    private IPayOrderService payOrderService;

    @Autowired
    private IAccountAlipayService accountAlipayService;

    /**
     * pc网站支付
     */
    @Override
    public String Pay(PcWebPayDTO dto)throws Exception {
        PayOrder payOrder = payOrderService.createPayOrder(dto);
        return pay(payOrder);
    }

    /**
     * 支付
     */
    private String pay(PayOrder payOrder)throws Exception {
        Integer payType = payOrder.getPayType();
        String res = "";
        if (payType == 1) {
            res = aliPay(payOrder);
        }
        return res;
    }

    /**
     * 支付宝的支付
     */
    private String aliPay(PayOrder payOrder) throws Exception{
        //构建支付宝支付的配置对象
        AccountAlipay accountAlipay = accountAlipayService.selectOne(
                new EntityWrapper<AccountAlipay>().eq("tenant_id", 0));
        AlipayConfig alipayConfig = new AlipayConfig();
        System.out.println(alipayConfig);
        alipayConfig.setServerUrl(accountAlipay.getProtocol()+"://"+accountAlipay.getGatewayHost());
        alipayConfig.setAppId(accountAlipay.getAppId());
        alipayConfig.setPrivateKey(accountAlipay.getMerchantPrivateKey());
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(accountAlipay.getAlipayPublicKey());
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType(accountAlipay.getSignType());
        //初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        //构造请求参数以调用接口
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //设置同步回调地址
        request.setReturnUrl(accountAlipay.getReturnUrl());
        //设置异步回调地址
        request.setNotifyUrl(accountAlipay.getNotifyUrl());
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        //设置订单标题
        model.setSubject(payOrder.getSubject());
        //设置产品码
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        //设置商户订单号
        model.setOutTradeNo(payOrder.getUniPayOrderSn());
        //设置订单总金额
        model.setTotalAmount(payOrder.getAmount().toString());
        request.setBizModel(model);
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "POST");
        String pageRedirectionData = response.getBody();
        System.out.println(pageRedirectionData);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return pageRedirectionData;
    }

}
